define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {

        index: function () {
            var html = '<div class="form-group">';
            html += '<label class="control-label col-xs-12 col-sm-6">请选择下载格式:</label>';
            html += '</div>';
            html += '<div class="form-group">';
            html += '<label class="control-label col-xs-12 col-sm-2"></label>';

            html += ' <div class="col-xs-12 col-sm-8">';
            html += ' <div class="radio">';
            html += '<label for="type-word"><input id="type-word" name="type" type="radio" value="word" checked /> Word文档</label>'
            html += '<label for="type-html"><input id="type-html" name="type" type="radio" value="html" /> HTML 文件</label>'
            html += '</div>';
            html += '</div>';
            html += '</div>';
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xilujob.resume/index' + location.search,
                    resumes_url: 'xilujob.resume/resumes' + location.search,
                    invite_url: 'xilujob.job_interview/create_interview',
                    del_url: '',
                    table: 'xilujob_resume',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'updatetime',
                search: false, //是否启用快速搜索
                commonSearch: true, //是否启用通用搜索
                searchFormVisible: true, //是否始终显示搜索表单
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate:false,visible:false},

                        {field: 'job.id', title: __('职位'), visible:false, searchList:Config.jobs},
                        // {field: 'age', title: __('年龄'),searchList: {'16,20': "16-20岁", '21,25':"21-25岁",'25,30':"25-30岁"},visible: false},
                        // {field: 'personal.gender', title: __('性别'),searchList: {'0': "未知", '1':"男",'2':"女"},visible: false},
                        // {field: 'personal.education_level_id', title: __('学历'),searchList: Config.education_level,visible: false},
                        // {field: 'jobIntention.expect_salary_id', title: __('期望薪资'),searchList: Config.expect_salary,visible: false},
                        // {field: 'personal.work_experience_id', title: __('工作经验'),searchList: Config.work_experience,visible: false},

                        {field: 'person',title:'基本信息',align:'left',formatter: Controller.api.formatter.personal, operate: false},
                        {field: 'work_experience', title: __('工作经验'),align:'left', formatter:Controller.api.formatter.experience, operate:false},
                        {field: 'operate', title: __('Operate'),buttons: [
                            {
                                name: 'communicate',
                                title: '立即沟通',
                                text: '立即沟通',
                                icon: '',
                                classname: 'btn btn-xs btn-warning btn-click',
                                click: function (event,row) {
                                    console.log(row);
                                    var message = {'type':'text','content':{text:'链接成功'}};
                                    var puser_id = row.user_id;
                                    var params = {message:message,type: 'service',puser_id: puser_id}
                                    XilujobChat.sendChatMessage(params);
                                }
                            },
                            {
                                name: 'detail',
                                title: '详情',
                                text: '详情',
                                icon: '',
                                classname: 'btn btn-xs btn-success btn-dialog',
                                url: 'xilujob.resume/detail'
                            },
                            {
                                name: 'interview',
                                title: '面试邀请',
                                text: '面试邀请',
                                icon: '',
                                extend: 'data-toggle="tooltip"',
                                classname: 'btn btn-xs btn-success btn-click',
                                click:  function (option,row,button) {
                                    Fast.api.ajax({
                                        url: Fast.api.fixurl('xilujob.resume/action_msg'),
                                        data:{type:'interview',resume_id:row.id}
                                    },function () {
                                        Fast.api.open($.fn.bootstrapTable.defaults.extend.invite_url+'?resume_id='+row.id,'面试邀请',{area:['578px','90%']});
                                        return false;
                                    }, function (data, ret) {
                                        Layer.confirm(ret.msg, {
                                            btn: ['立即购买','稍后再说']
                                        },function (index) {
                                            location.href = Fast.api.fixurl("xilujob.package/index");
                                        });
                                        return false;
                                    });
                                }
                            },
                            {
                                name: 'download',
                                title: '下载简历',
                                text: '下载简历',
                                extend: 'data-toggle="tooltip"',
                                classname: 'btn btn-xs btn-success btn-click',
                                icon: '',
                                click: function (option,row,button) {

                                    Fast.api.ajax({
                                        url: Fast.api.fixurl('xilujob.resume/action_msg'),
                                        data:{type:'resume_view',resume_id:row.id}
                                    },function () {
                                        layer.open({
                                            //formType: 2,//这里依然指定类型是多行文本框，但是在下面content中也可绑定多行文本框
                                            title: '下载简历',
                                            area: ['400px', '240px'],
                                            btnAlign: 'c',
                                            closeBtn:'1',//右上角的关闭
                                            content: html,
                                            btn:['确认','关闭'],
                                            yes: function (index, layero) {
                                                var type = $(":checked[name=type]").val();
                                                if(!row.id){
                                                    Toastr.error('未找到简历id');
                                                    return false;
                                                }
                                                location.href = Fast.api.fixurl('xilujob.resume/download?type='+type+'&ids='+row.id)
                                                return false;
                                            },
                                        });
                                        return false;
                                    }, function (data, ret) {
                                        Layer.confirm(ret.msg, {
                                            btn: ['立即购买','稍后再说']
                                        },function (index) {
                                            location.href = Fast.api.fixurl("xilujob.package/index");
                                        });
                                        return false;
                                    });
                                },
                            },
                            ], table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            //批量邀请
            $('.batch-invite').on('click', function () {
                var ids = Table.api.selectedids(table);
                //先判断
                Fast.api.ajax({
                    url: Fast.api.fixurl('xilujob.resume/action_msg'),
                    data:{type:'interview',resume_ids:ids.join(',')}
                },function () {
                    Fast.api.open(Fast.api.fixurl("xilujob.job_interview/batch_invite")+'?resume_ids='+ids.join(','),'面试邀请',{area:['578px','90%']});
                    return false;
                }, function (data, ret) {
                    Layer.confirm(ret.msg, {
                        btn: ['立即购买','稍后再说']
                    },function (index) {
                        location.href = Fast.api.fixurl("xilujob.package/index");
                    });
                    return false;
                });
            });

            //批量下载简历
            $('.batch-download').on('click', function () {
                var ids = Table.api.selectedids(table);
                //检测次数
                Fast.api.ajax({
                    url: 'xilujob.resume/action_msg',
                    data: {type:'resume_view',resume_ids: ids.join(',')}
                }, function (data, ret) {
                    layer.open({
                        //formType: 2,//这里依然指定类型是多行文本框，但是在下面content中也可绑定多行文本框
                        title: '下载简历',
                        area: ['600px', '400px'],
                        btnAlign: 'c',
                        closeBtn:'1',//右上角的关闭
                        content: html,
                        btn:['确认','关闭'],
                        yes: function (index, layero) {
                            var type = $(":checked[name=type]").val();
                            if(!ids){
                                Toastr.error('未找到简历id');
                                return false;
                            }
                            location.href = Fast.api.fixurl('xilujob.resume/batch_download?type='+type+'&ids='+ids.join(','))
                            return false;
                        }
                    });
                    return false;
                }, function (data, ret) {
                    Layer.confirm(ret.msg, {
                        btn: ['立即购买','稍后再说']
                    },function (index) {
                        location.href = Fast.api.fixurl("xilujob.package/index");
                    });
                    return false;
                });
            });


        },

        resumes: function () {

            var html = '<div class="form-group">';
            html += '<label class="control-label col-xs-12 col-sm-6">请选择下载格式:</label>';
            html += '</div>';
            html += '<div class="form-group">';
            html += '<label class="control-label col-xs-12 col-sm-2"></label>';

            html += ' <div class="col-xs-12 col-sm-8">';
            html += ' <div class="radio">';
            html += '<label for="type-word"><input id="type-word" name="type" type="radio" value="word" checked /> Word文档</label>'
            html += '<label for="type-html"><input id="type-html" name="type" type="radio" value="html" /> HTML 文件</label>'
            html += '</div>';
            html += '</div>';
            html += '</div>';

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xilujob.resume/resumes' + location.search,
                    invite_url: 'xilujob.job_interview/create_interview',
                    del_url: '',
                    table: 'xilujob_resume',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'updatetime',
                search: false, //是否启用快速搜索
                commonSearch: true, //是否启用通用搜索
                searchFormVisible: true, //是否始终显示搜索表单
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate:false,visible:false},
                        {field: 'job.id', title: __('职位'), visible:false, searchList:Config.jobs},
                        // {field: 'job_intention.province_id', title: __('省份'),searchList:function () {
                        //         return Template('categorytpl',{});
                        //     },visible: false},
                        // {field: 'job_intention.fav_category_id', title: __('职位类型'),searchList:function () {
                        //         return Template('categorytpl2',{});
                        //     },visible: false},
                        {field: 'age', title: __('年龄'),searchList: {'16,20': "16-20岁", '21,25':"21-25岁",'25,30':"25-30岁"},visible: false},
                        {field: 'personal.gender', title: __('性别'),searchList: {'0': "未知", '1':"男",'2':"女"},visible: false},
                        {field: 'personal.education_level_id', title: __('学历'),searchList: Config.education_level,visible: false},
                        // {field: 'jobIntention.expect_salary_id', title: __('期望薪资'),searchList: Config.expect_salary,visible: false},
                        {field: 'personal.work_experience_id', title: __('工作经验'),searchList: Config.work_experience,visible: false},

                        {field: 'person',title:'基本信息',align:'left',formatter: Controller.api.formatter.personal, operate: false},
                        {field: 'work_experience', title: __('工作经验'),align:'left', formatter:Controller.api.formatter.experience, operate:false},

                        {field: 'operate', title: __('Operate'),buttons: [
                                {
                                    name: 'detail',
                                    title: '详情',
                                    text: '详情',
                                    icon: '',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: 'xilujob.resume/detail'
                                },
                                {
                                    name: 'interview',
                                    title: '面试邀请',
                                    text: '面试邀请',
                                    icon: '',
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-xs btn-success btn-click',
                                    click:  function (option,row,button) {
                                        Fast.api.ajax({
                                            url: Fast.api.fixurl('xilujob.resume/action_msg'),
                                            data:{type:'interview',resume_id:row.id}
                                        },function () {
                                            Fast.api.open($.fn.bootstrapTable.defaults.extend.invite_url+'?resume_id='+row.id,'面试邀请',{area:['578px','90%']});
                                            return false;
                                        }, function (data, ret) {
                                            Layer.confirm(ret.msg, {
                                                btn: ['立即购买','稍后再说']
                                            },function (index) {
                                                location.href = Fast.api.fixurl("xilujob.package/index");
                                            });
                                            return false;
                                        });
                                    }
                                },
                                {
                                    name: 'download',
                                    title: '下载简历',
                                    text: '下载简历',
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-xs btn-success btn-click',
                                    icon: '',
                                    click: function (option,row,button) {

                                        Fast.api.ajax({
                                            url: Fast.api.fixurl('xilujob.resume/action_msg'),
                                            data:{type:'resume_view',resume_id:row.id}
                                        },function () {
                                            layer.open({
                                                //formType: 2,//这里依然指定类型是多行文本框，但是在下面content中也可绑定多行文本框
                                                title: '下载简历',
                                                area: ['400px', '240px'],
                                                btnAlign: 'c',
                                                closeBtn:'1',//右上角的关闭
                                                content: html,
                                                btn:['确认','关闭'],
                                                yes: function (index, layero) {
                                                    var type = $(":checked[name=type]").val();
                                                    if(!row.id){
                                                        Toastr.error('未找到简历id');
                                                        return false;
                                                    }
                                                    location.href = Fast.api.fixurl('xilujob.resume/download?type='+type+'&ids='+row.id)
                                                    return false;
                                                },
                                            });
                                            return false;
                                        }, function (data, ret) {
                                            Layer.confirm(ret.msg, {
                                                btn: ['立即购买','稍后再说']
                                            },function (index) {
                                                location.href = Fast.api.fixurl("xilujob.package/index");
                                            });
                                            return false;
                                        });
                                    },
                                },
                            ], table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                queryParams: function (params) {
                    //这里可以追加搜索条件
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    //这里可以动态赋值，比如从URL中获取admin_id的值，filter.admin_id=Fast.api.query('admin_id');
                    // filter.admin_id = 1;
                    // op.admin_id = "=";
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            //批量邀请
            $('.batch-invite').on('click', function () {
                var ids = Table.api.selectedids(table);
                //先判断
                Fast.api.ajax({
                    url: Fast.api.fixurl('xilujob.resume/action_msg'),
                    data:{type:'interview',resume_ids:ids.join(',')}
                },function () {
                    Fast.api.open(Fast.api.fixurl("xilujob.job_interview/batch_invite")+'?resume_ids='+ids.join(','),'面试邀请',{area:['578px','90%']});
                    return false;
                }, function (data, ret) {
                    Layer.confirm(ret.msg, {
                        btn: ['立即购买','稍后再说']
                    },function (index) {
                        location.href = Fast.api.fixurl("xilujob.package/index");
                    });
                    return false;
                });
            });

            //批量下载简历
            $('.batch-download').on('click', function () {
                var ids = Table.api.selectedids(table);
                //检测次数
                Fast.api.ajax({
                    url: 'xilujob.resume/action_msg',
                    data: {type:'resume_view',resume_ids: ids.join(',')}
                }, function (data, ret) {
                    layer.open({
                        //formType: 2,//这里依然指定类型是多行文本框，但是在下面content中也可绑定多行文本框
                        title: '下载简历',
                        area: ['600px', '400px'],
                        btnAlign: 'c',
                        closeBtn:'1',//右上角的关闭
                        content: html,
                        btn:['确认','关闭'],
                        yes: function (index, layero) {
                            var type = $(":checked[name=type]").val();
                            if(!ids){
                                Toastr.error('未找到简历id');
                                return false;
                            }
                            location.href = Fast.api.fixurl('xilujob.resume/batch_download?type='+type+'&ids='+ids.join(','))
                            return false;
                        }
                    });
                    return false;
                }, function (data, ret) {
                    Layer.confirm(ret.msg, {
                        btn: ['立即购买','稍后再说']
                    },function (index) {
                        location.href = Fast.api.fixurl("xilujob.package/index");
                    });
                    return false;
                });
            });
        },
        detail: function(){
            //邀请面试
            $('.batch-invite').on('click', function () {
                var resume_id = $(this).data('resume_id') || '';
                if(!resume_id){
                    Toastr.error('未找到简历')
                    return false;
                }
                var apply_id = Config.apply_id || 0;
                //检测次数
                Fast.api.ajax({url:'xilujob.resume/action_msg',data:{type:'interview',resume_id: resume_id}}, function (data, ret) {
                    var url = 'xilujob.job_interview/create_interview?apply_id='+apply_id+'&resume_id='+resume_id;
                    Fast.api.open(url,'面试邀请');
                    return false;
                }, function (data, ret) {
                    Layer.confirm(ret.msg, {
                        btn: ['立即购买','稍后再说']
                    },function (index) {
                        location.href = Fast.api.fixurl("xilujob.package/index");
                    });
                    return false;
                });
                return false;
            });
            $(document).on('click','.btn-collection',function () {
                var that = $(this);
                var is_fav = $(this).data('is_fav');
                var resume_id = $(this).data('resume_id') || '';
                if(!resume_id){
                    Toastr.error('未找到简历')
                    return false;
                }
                var tips = is_fav?'取消收藏':'收藏';
                //收藏（取消）收藏
                layer.confirm('确认'+tips,{},function (index) {
                    Fast.api.ajax({url:'xilujob.resume/toggle_fav',data:{id: resume_id}}, function (data, ret) {
                        console.log(data)
                        that.text(!data.is_fav?'收藏':'已收藏');
                        Toastr.success(data.is_fav?"收藏成功":'取消收藏成功');
                        layer.close(index);
                        return false;
                    },function () {
                        layer.close(index);
                    });


                })

            })
        },
        api: {
            formatter:{
                personal: function(value,row) {
                    var resume_data = row.age+' | '
                        + row.work_experience_name + ' | '
                        + row.education_level_name + ' | '
                        + row.current_state_name + ' | '
                        +(row.job_type==1?'全职':'兼职');
                    var summary_content = row.summary.content;
                    summary_content = summary_content.length>20 ? summary_content.substring(0,20)+'...' : summary_content;
                    var html = '<div class="media">';
                    html += '<a class="media-left" href="#">';
                    html += '<img width="50px" height="50px" src="'+Fast.api.cdnurl(row.avatar)+'" class="img-circle" alt="">';
                    html += '<div>'+row.salary_name+'</div>';
                    html += '</a>';

                    html += '<div class="media-body" style="width: auto">';
                    html += '<h4>'+row.realname+'</h4>';
                    html += '<p>'+resume_data+'</p>';
                    html += '<p>求职期望：'+row.allname+'</p>';
                    html += '<p><a title="'+row.summary.content+'" style="width: 150px">'+summary_content+'</a></p>';
                    html += '</div>';
                    html += '</div>';
                    return html;
                },
                experience: function(value,row){
                    if(!row.works) return '';
                    var html = '';
                    for (var i=0;i<row.works.length;i++){
                        var end = row.works[i].end ? row.works[i].end : "至今";
                        html += '<h5>'+row.works[i].start+'-'+end+'   '+row.works[i].company_name+' . '+row.works[i].job_title+'</h5>';
                    }
                    return html;
                }
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }

    };
    return Controller;
});